package de.ipk_gatersleben.bit.bi.edal.publication.attribute.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;

import de.ipk_gatersleben.bit.bi.edal.publication.AttributeLabel;
import de.ipk_gatersleben.bit.bi.edal.publication.AttributeTablePanel;
import de.ipk_gatersleben.bit.bi.edal.publication.PropertyLoader;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationFrame;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationMainPanel;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationModul;
import de.ipk_gatersleben.bit.bi.edal.publication.Utils;

public class LanguagePanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 8109825692298261311L;

	private static AttributeLabel LANGUAGE_LABEL = new AttributeLabel(
			PropertyLoader.props.getProperty("LANGUAGE_LABEL"),
			PropertyLoader.props.getProperty("LANGUAGE_TOOLTIP"));

	private static JButton OKAY_BUTTON = new JButton("OK");

	private static JComboBox<String> comboBox;

	private static Locale[] LOCALE_LIST;

	static {

		LOCALE_LIST = Locale.getAvailableLocales();

		Arrays.sort(LOCALE_LIST, new Comparator<Locale>() {

			@Override
			public int compare(Locale o1, Locale o2) {
				return o1.toString().compareTo(o2.toString());
			}
		});

		String[] strings = new String[LOCALE_LIST.length];

		for (int i = 0; i < LOCALE_LIST.length; i++) {

			if (!LOCALE_LIST[i].getDisplayCountry().isEmpty()) {
				strings[i] = LOCALE_LIST[i].getDisplayLanguage() + " - "
						+ LOCALE_LIST[i].getDisplayCountry();
			} else {
				strings[i] = LOCALE_LIST[i].getDisplayLanguage();
			}
		}

		comboBox = new JComboBox<String>(strings);

		comboBox.setBorder(BorderFactory.createEmptyBorder());

		comboBox.setSelectedItem(PublicationMainPanel.DEFAULT_LANGUAGE_STRING);
	}

	public LanguagePanel() {

		JPanel mainPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));

		mainPanel.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);

		comboBox.setSelectedItem(PublicationMainPanel.DEFAULT_LANGUAGE_STRING);

		mainPanel.add(comboBox);
		mainPanel.add(OKAY_BUTTON);

		OKAY_BUTTON.addActionListener(this);

		JPanel attributePanel = new JPanel(new BorderLayout());

		attributePanel.add(LANGUAGE_LABEL, BorderLayout.NORTH);
		attributePanel.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);
		attributePanel.setPreferredSize(new Dimension(50,
				PropertyLoader.DEFAULT_FONT_SIZE));

		this.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));

		GridBagLayout gridbaglayout = new GridBagLayout();

		this.setLayout(gridbaglayout);
		this.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);
		this.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));

		Utils.addComponent(this, gridbaglayout, attributePanel, 0, 0, 1, 1,
				0.02, 1, 50, 10);
		Utils.addComponent(this, gridbaglayout, mainPanel, 1, 0, 1, 1, 0.98, 1,
				50, 10);

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {

		if (actionEvent.getSource().equals(OKAY_BUTTON)) {

			PublicationMainPanel.languagePanel = this;

			PublicationMainPanel.languageField.setText(getTableContent());

			PublicationMainPanel.releaseAllBlockedFields();

			PublicationMainPanel.getMainPanel().remove(
					PublicationMainPanel.LANGUAGE_PANEL_COMPONENT_NUMBER);

			PropertyLoader.LANGUAGE_LABEL
					.setForeground(PropertyLoader.LABEL_COLOR);

			PublicationMainPanel.getMainPanel().add(
					new AttributeTablePanel(PropertyLoader.LANGUAGE_LABEL,
							PublicationMainPanel.languageField,
							PropertyLoader.DEFAULT_FONT_SIZE),
					PublicationMainPanel.LANGUAGE_PANEL_COMPONENT_NUMBER);

			PublicationFrame.updateUI();
			PublicationModul.getFrame().requestFocusInWindow();

		}

	}

	private String getTableContent() {
		return LanguagePanel.comboBox.getSelectedItem().toString();
	}

	public Locale getLanguage() {
		return LOCALE_LIST[LanguagePanel.comboBox.getSelectedIndex()];
	}
}